import wsgiref.handlers
import cgi

from google.appengine.ext import webapp
#from google.appengine.api import users

import shoutbox.views
import gallery.views
import usercounter.views
import ytalk.views
import fridge.views
import utils

import settings

class MainPage(webapp.RequestHandler):
  def get(self):
    self.response.out.write(utils.render_to_mako('main.html', 
                {'servicesurl':settings.SERVICESURL, 
            'domain':settings.DOMAIN,}))



class addslash(webapp.RequestHandler):
  def get(self):
      qs = self.request.environ.get('QUERY_STRING', '')
      if qs:
          qs = '?'+ qs
      self.redirect( self.request.environ['PATH_INFO'] + '/' + qs )

application = webapp.WSGIApplication(
                                       [('/', MainPage),
                                        ('/gallery',           addslash),
                                        ('/gallery/',           gallery.views.List),
                                        ('/gallery/([^_]*)(_small\.|_full\.|\.)(jpg|jpeg|png|tif|tiff|gif)',      gallery.views.ServeImage),
                                        ('/gallery/([^/]*)/(detail|full)',      gallery.views.ServeDetails),
                                        ('/gallery/([^/]*)/upload',      gallery.views.UploadImage),

                                        ('/shoutbox',           addslash),
                                        ('/shoutbox/',            shoutbox.views.Main),
                                        ('/shoutbox/channel_uri', shoutbox.views.ChannelUri),
                                        ('/shoutbox/user_action', shoutbox.views.UserAction),
                                        ('/shoutbox/push',            shoutbox.views.Push),

                                        ('/usercounter',            addslash),
                                        ('/usercounter/',            usercounter.views.Main),
                                        ('/usercounter/channel_uri', usercounter.views.ChannelUri),
                                        
                                        ('/ytalk',                          addslash),
                                        ('/ytalk/',                         ytalk.views.ListRooms),
                                        ('/ytalk/contents',                 ytalk.views.ListRoomsPartial),
                                        ('/ytalk/channel_uri',              ytalk.views.ListRoomsChannelUri),
                                        ('/ytalk/([^/]*)',                 addslash),
                                        ('/ytalk/([^/]*)/',                ytalk.views.RoomDetails),
                                        ('/ytalk/([^/]*)/channel_uri',     ytalk.views.RoomChannelUri),
                                        ('/ytalk/([^/]*)/user_action',     ytalk.views.RoomUserAction),
                                        
                                        ('/fridge',                         addslash),
                                        ('/fridge/',                        fridge.views.FridgeMain),
                                        ('/fridge/([^_]*)_([0-9x]*)',       fridge.views.FridgeGetImage),
                                        ('/fridge/upload/([^_]*)',          fridge.views.FridgeUploadImage),
                                        ('/fridge/channel_uri',             fridge.views.FridgeMagnetChannelUri),
                                        ('/fridge/user_action',             fridge.views.FridgeMagnetUserAction),
                                        
                                        ],
                                       debug=True)

def main():
  wsgiref.handlers.CGIHandler().run(application)

if __name__ == "__main__":
  main()